class TagTests extends TestHelper {

  def tag

  protected void setUp() {
    super.setUp();
    tag = new Tag(name:"foo")
  }

  void testATagCanBeSaved() {
    assertNotNull(new Tag(name:"monkey").save())
  }

  void testNameCannotBeNull() {
    fieldCannotBeNull(tag, "name")
  }

  void testNameCannotBeEmpty() {
    fieldCannotBeEmpty(tag, "name")
  }

  void testNameCanBe20Characters() {
    fieldLengthValid(tag, "name", 20)
  }

  void testNameCannotBeMoreThan20Characters() {
    fieldLengthInvalid(tag, "name", 21)    
  }

  void testNameMustBeUnique() {
    assertNotNull(new Tag(name:"name").save())
    assertNull(new Tag(name:"name").save())
  }

  void testPostsCanBeRetrievedForATag() {
    Tag code = new Tag(name:"code")
    Tag testing = new Tag(name:"testing")

    def post = new Post(
        title:"1st Post",
        teaser:"bar",
        content:"foo bar",
        published:false)
        .addToTags(code).save()
    def post2 = new Post(
        title:"2nd Post",
        teaser:"bar",
        content:"foo bar",
        published:false)
        .addToTags(code)
        .addToTags(testing).save()
        
    assertEquals(2, Tag.findByName("code").posts.size())
    assertEquals(1, Tag.findByName("testing").posts.size())
  }
}